<?php
namespace App\Http\Controllers\Passenger\V2\Order;

use App\Http\Controllers\Controller;
use App\Models\Comment;
use App\Models\Drivers;
use App\Services\PassengerCostService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;
use Reprover\Amap\Amap;
use Reprover\Amap\Exceptions\InvalidArgumentException;

/**
 * 资费相关
 * Class CostController
 * @package App\Http\Controllers\Passenger\V2\Order
 */
class CostController extends Controller
{


    /**
     * 获取预计费用
     * @param Request $request
     * @param PassengerCostService $service
     * @return CostController|\Illuminate\Http\JsonResponse
     */
    public function getPredict(Request $request, PassengerCostService $service)
    {

        $origin           = $request->post('origin');
        $origin_name      = $request->post('origin_name');
        $destination      = $request->post('destination');
        $destination_name = $request->post('destination_name');
        $boarding_time    = time();//todo 预约时间选择

        if( !($origin && $destination && $origin_name && $destination_name) )
            return $this->sendError('起始位置或重点位子不能为空');

        $cost_data                     = [];
        $order_no                      = uniqid();
        $cost_data['user_id']          = Auth::id();
        $cost_data['order_no']         = $order_no;
        $cost_data['type']             = 0;  //1.代驾  0.网约车
        $cost_data['boarding_time']    = date('Y-m-d H:i', $boarding_time);
        $cost_data['origin']           = $origin;
        $cost_data['origin_name']      = $origin_name;
        $cost_data['destination']      = $destination;
        $cost_data['destination_name'] = $destination_name;
        $cost_data['types'] = 0;
        $cost_data                    += $service->getPredictByLoLa($origin, $destination, $boarding_time);

        $redis = app("redis.connection");
        $redis->setex($order_no, 86400, json_encode($cost_data));

        return $this->sendJson($cost_data);

    }

    /**
     * 代驾资费获取
     */
    public function driving_predict(Request $request, PassengerCostService $service)
    {
        $origin           = $request->post('origin');
        $origin_name      = $request->post('origin_name');
        $destination      = $request->post('destination');
        $destination_name = $request->post('destination_name');
        $types = (int)$request->post('type',0); // 0 日常驾 1 酒后驾  2 商务驾  more:日常驾
        $boarding_time    = time();//todo 预约时间选择

        if( !($origin && $destination && $origin_name && $destination_name) )
            return $this->sendError('起始位置或重点位子不能为空');
        $cost_data                     = [];
        $order_no                      = uniqid();
        $cost_data['user_id']          = Auth::id();
        $cost_data['order_no']         = $order_no;
        $cost_data['type']             = 1;  //1.代驾  0.网约车
        $cost_data['boarding_time']    = date('Y-m-d H:i', $boarding_time);
        $cost_data['origin']           = $origin;
        $cost_data['origin_name']      = $origin_name;
        $cost_data['destination']      = $destination;
        $cost_data['destination_name'] = $destination_name;
        $cost_data['types'] = $types;
        $cost_data                    += $service->driving_tarff($origin, $destination, $boarding_time);

        $redis = app("redis.connection");
        $redis->setex($order_no, 86400, json_encode($cost_data));

        return $this->sendJson($cost_data);
    }
}
